package com.whynot.frame.hystrix.controller;

import com.whynot.frame.hystrix.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描    述:熔断rest模拟多并发请求
 * 创建时间: 2018/12/21
 */
@RestController
public class HystrixController {

    @Autowired
    HystrixService hystrixService;

    @RequestMapping("/test")
    public String test() throws InterruptedException {

       /*

            int threadCount = 200;
            ExecutorService es = Executors.newFixedThreadPool(threadCount);//线程池
            CountDownLatch cdl = new CountDownLatch(threadCount);//计数器，发令枪


            for (int i = 0; i < threadCount; i++)
            {
                es.execute(new Runnable(){

                    @Override
                    public void run(){
                        System.out.println("当前线程:" + Thread.currentThread().getName() + "准备就绪, 等待countdown为0后开始运行....");

                        try {
                            cdl.await();//发令枪等待
                        } catch (InterruptedException e1) {

                        }
                        finally
                        {
                            System.out.println("当前线程:" + Thread.currentThread().getName() + "等待结束, 开始模拟web用户请求， 开始时间:" + System.currentTimeMillis());
                        }

                        //模拟准备500ms
                        //try {
                       *//* System.out.println("测试");
                        System.out.println("对象"+robotKfHandler);
                        System.out.println("对象2"+wxMpXmlMessage);*//*
                        try{
                            System.out.println("测试");
                            System.out.println(hystrixService.getReplyByRobot());
                            System.out.println("测试2");
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                        //Thread.sleep(500);
                    *//*} catch (InterruptedException e) {
                        System.out.println(e);
                        e.printStackTrace();
                    }
                    finally
                    {
                        System.out.println("当前线程:" + Thread.currentThread().getName() + "模拟over....");
                    }*//*
                    }
                });
            }

            //等待两秒，等待并发线程初始化完成。
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("主线程:" + Thread.currentThread().getName() + "countdown自减开始。。。");
            for (int j = 0; j < threadCount; j++)
            {
                cdl.countDown();
            }
            System.out.println("主线程:" + Thread.currentThread().getName() + "countdown自减完成。。。");

            es.shutdown();
            */

        return hystrixService.getReplyByRobot();
    }
}